package com.simon.system.service.impl;


import java.util.Arrays;
import java.util.Collections;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.simon.system.dao.PermissonMapper;
import com.simon.system.model.Permisson;
import com.simon.system.service.PermissonService;

@Service
public class PermissonServiceImpl implements PermissonService {
	
	@Autowired
	private PermissonMapper permissonMapper;
	
	@Override
	public List<Permisson> findByUserId(Integer userId) {
		List<Permisson> permissons = permissonMapper.findByUserId(userId);
		PcodeLevel pcodeLevel = null;
		for (Permisson permisson : permissons) {
			pcodeLevel = getPcodes(permisson);
			permisson.setPcodes(pcodeLevel.getPcodes());
			permisson.setLevels(pcodeLevel.getLevels());
		}
		
		return permissons;
	}

	@Override
	public int addMenu(Permisson menu) {
		return permissonMapper.insert(menu);
	}

	@Override
	public Permisson getMenuById(int id) {
		Permisson permisson = permissonMapper.selectByPrimaryKey(id);
		PcodeLevel pcodeLevel = getPcodes(permisson);
		permisson.setPcodes(pcodeLevel.getPcodes());
		permisson.setLevels(pcodeLevel.getLevels());
		return permisson;
	}

	@Override
	public int deleteMenu(int id) {
		return permissonMapper.deleteByPrimaryKey(id);
	}

	@Override
	public int updateMenu(Permisson oldMenu, Permisson newMenu) {
		newMenu.setId(oldMenu.getId());
		return permissonMapper.updateByPrimaryKey(newMenu);
	}

	@Override
	public List<Permisson> findAllMenus() {
		List<Permisson> permissons = permissonMapper.selectPermissons();
		PcodeLevel pcodeLevel;
		for (Permisson permisson : permissons) {
			pcodeLevel = getPcodes(permisson);
			permisson.setPcodes(pcodeLevel.getPcodes());
			permisson.setLevels(pcodeLevel.getLevels());
		}
		return permissons;
	}
	
	
	/**
	 * 获取菜单的父级节点，按[system,user]组成
	 * 获取菜单的层级，顶级为1级
	 * @param permisson
	 * @return
	 */
	PcodeLevel getPcodes(Permisson permisson) {		
		StringBuilder sbPcodes = new StringBuilder("");
		getPcodesByStep(permisson, sbPcodes);
		//存在父节点
		if (sbPcodes.length() > 0) {
			sbPcodes.deleteCharAt(sbPcodes.length()-1);
		
			String[] arr = sbPcodes.toString().split(",");
			Collections.reverse(Arrays.asList(arr));	
			return new PcodeLevel(Arrays.toString(arr),arr.length + 1);
		}
		
		return new PcodeLevel(null,1);
	}
	
	/**
	 * 递归调用-获取父级节点
	 * @param permisson
	 * @param sbPcodes
	 */
	void getPcodesByStep(Permisson permisson,StringBuilder sbPcodes) {
		//获取当前节点的pcode
		String pcode = permisson.getPcode();
		
		if(pcode!=null && !"".equals(pcode)) {
			sbPcodes.append(pcode).append(",");
			//根据当前节点的pcode获取父节点
			Permisson menu = permissonMapper.findByCode(pcode);
			
			getPcodesByStep(menu,sbPcodes);
		}
		
	}
	
	/**
	 * 根据角色id获取权限列表
	 */
	@Override
	public List<Permisson> findByRoleId(Integer roleId) {
		List<Permisson> permissons = permissonMapper.findByRoleId(roleId);
		PcodeLevel pcodeLevel = null;
		for (Permisson permisson : permissons) {
			pcodeLevel = getPcodes(permisson);
			permisson.setPcodes(pcodeLevel.getPcodes());
			permisson.setLevels(pcodeLevel.getLevels());
		}
		return permissons;
	}

	@Override
	public Permisson findByCode(String code) {
		return permissonMapper.findByCode(code);
	}
	

}

/**
 * 处理菜单的父节点集和菜单层级
 * @author Jiajie.Mao
 *
 */
class PcodeLevel{
	
	private String pcodes;
	
	private int levels;
	
	public PcodeLevel() {
		
	}

	public PcodeLevel(String pcodes, int levels) {
		super();
		this.pcodes = pcodes;
		this.levels = levels;
	}

	public String getPcodes() {
		return pcodes;
	}

	public void setPcodes(String pcodes) {
		this.pcodes = pcodes;
	}

	public int getLevels() {
		return levels;
	}

	public void setLevels(int levels) {
		this.levels = levels;
	}
	
	
}
